import 'package:dio/dio.dart';
import 'package:flower/components/custom_toast.dart';
import 'package:flower/config/service_url.dart';
import 'package:flower/model/base_model.dart';
import 'package:flower/utils/http_util.dart';
import 'package:flutter/material.dart';

class CommentAdd extends StatefulWidget {
  final String number;
  const CommentAdd({super.key, required this.number});
  @override
  State<StatefulWidget> createState() {
    return _CommentAdd();
  }
}

class _CommentAdd extends State<CommentAdd> {
  final TextEditingController _descriptionController = TextEditingController();
  int _selectedStarCount = 0;

  void submitComment() async {
    if (_descriptionController.text.isNotEmpty && _selectedStarCount > 0) {
      String content = _descriptionController.text;
      int stars = _selectedStarCount;

      Map<String, dynamic> map = <String, dynamic>{};
      map["content"] = content;
      map["stars"] = stars;
      map["number"] = widget.number;

      Response response = await HttpUtil.post(addComment, queryParameters: map);
      BaseModel baseModel = BaseModel.fromJson(response.data);
      if (baseModel.code == 1) {
        CustomToast.showWarningToast("评价成功");
        _descriptionController.clear();
        setState(() {
          _selectedStarCount = 0;
        });
        Navigator.pop(context);
      }
    } else {
      CustomToast.showWarningToast("请填写评价并选择星级");
    }
  }

  void _onStarTap(int starCount) {
    setState(() {
      _selectedStarCount = starCount;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('发表评价'),
        centerTitle: true,
      ),
      body: ListView(
        children: [
          Container(
            margin: const EdgeInsets.all(10),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: List.generate(5, (index) {
                return GestureDetector(
                  onTap: () => _onStarTap(index + 1),
                  child: Icon(
                    _selectedStarCount > index ? Icons.star : Icons.star_border,
                    size: 30,
                    color: Colors.yellow,
                  ),
                );
              }),
            ),
          ),
          Container(
            margin: const EdgeInsets.all(20),
            child: TextField(
              controller: _descriptionController,
              decoration: const InputDecoration(
                labelText: '亲~请公主评价',
                labelStyle: TextStyle(fontSize: 15),
                border: OutlineInputBorder(),
                contentPadding:
                    EdgeInsets.symmetric(vertical: 10, horizontal: 15),
              ),
              style: const TextStyle(fontSize: 17),
              maxLines: 10,
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: submitComment,
                child: const Text('评价'),
              ),
              const SizedBox(width: 60),
              ElevatedButton(
                onPressed: () {
                  Navigator.of(context).pop();
                },
                child: const Text('取消'),
              ),
            ],
          )
        ],
      ),
    );
  }
}
